package com.aat.high.datasource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.logicalcobwebs.proxool.ProxoolDataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class TestDataSourceMain {
	private static String sql = "SELECT nickname FROM base_user u WHERE u.account='admin'";
	private static DruidDataSource dataS;
	//
	private static String driver = "com.mysql.jdbc.Driver";
	private static String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";
	private static String user = "root";
	private static String passwd = "root";

	private static int count = 10000;
	private static HikariDataSource dataS2;

	public static void main(String[] args) throws Exception {
		long l1 = System.currentTimeMillis();
		testC3p0();
		long l2 = System.currentTimeMillis();

		long l3 = System.currentTimeMillis();
		testDruid();
		long l4 = System.currentTimeMillis();

		long l5 = System.currentTimeMillis();
		testProxool();
		long l6 = System.currentTimeMillis();
		
		long l7 = System.currentTimeMillis();
		testHikar();
		long l8 = System.currentTimeMillis();

		System.out.println("c3p0   :" + (l2 - l1));
		System.out.println("druid  :" + (l4 - l3));
		System.out.println("proxool:" + (l6 - l5));
		System.out.println("hikarCP:" + (l8 - l7));
	}

	public static void testC3p0() throws Exception {
		ComboPooledDataSource dataS = new ComboPooledDataSource();
		// config
		dataS.setDriverClass(driver);
		dataS.setJdbcUrl(jdbcUrl);
		dataS.setUser(user);
		dataS.setPassword(passwd);
		dataS.setMaxPoolSize(100);
		dataS.setMinPoolSize(1);

		// action
		Connection conn = dataS.getConnection();
		int i = 0;
		while (i < count) {
			i++;
			query(conn);
		}
		dataS.close();
	}

	public static void testDruid() throws Exception {
		dataS = new DruidDataSource();
		// config
		dataS.setDriverClassName(driver);
		dataS.setUrl(jdbcUrl);
		dataS.setUsername(user);
		dataS.setPassword(passwd);
		dataS.setMaxActive(100);
		dataS.setMinIdle(1);
		dataS.setInitialSize(1);

		// action
		Connection conn = dataS.getConnection();
		int i = 0;
		while (i < count) {
			i++;
			query(conn);
		}
		dataS.clone();
	}

	public static void testProxool() throws Exception {
		ProxoolDataSource dataS = new ProxoolDataSource();
		// config
		dataS.setDriver(driver);
		dataS.setDriverUrl(jdbcUrl);
		dataS.setUser(user);
		dataS.setPassword(passwd);
		dataS.setMaximumConnectionCount(100);
		dataS.setMinimumConnectionCount(1);

		// action
		Connection conn = dataS.getConnection();
		int i = 0;
		while (i < count) {
			i++;
			query(conn);
		}
	}

	public static void testHikar() throws Exception  {
		HikariConfig config = new HikariConfig();
		config.setMinimumIdle(1);
		config.setMaximumPoolSize(100);
		config.setDriverClassName(driver);
		config.setJdbcUrl(jdbcUrl);
		config.setUsername(user);
		config.setPassword(passwd);
		dataS2 = new HikariDataSource(config);

		// action
		Connection conn = dataS2.getConnection();
		int i = 0;
		while (i < count) {
			i++;
			query(conn);
		}
	}

	public static void query(Connection conn) {
		try {
			Statement st = conn.createStatement();
			ResultSet result = st.executeQuery(sql);
			while (result.next()) {
				result.getString("nickname");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
